package com.king.modules.system.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.king.common.annotation.MvcData;
import com.king.framework.base.domain.vo.AjaxResult;
import com.king.modules.system.domain.vo.file.FileUploadDto;
import com.king.modules.system.domain.vo.file.FileUploadVo;
import com.king.modules.system.domain.vo.file.ImgUploadDto;
import com.king.modules.system.domain.vo.file.MultiFilesUploadDto;
import com.king.modules.system.domain.vo.file.MultiImgsUploadDto;
import com.king.modules.system.service.impl.FileService;

/**
 * Description:
 *
 * @author Xander
 * datetime: 2023-03-10 13:58
 */
@RestController
@RequestMapping(value = "/system/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 上传图片
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/uploadImg", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult uploadImg(@ModelAttribute ImgUploadDto dto) throws Exception {
        FileUploadVo uploadVo = this.fileService.uploadImg(dto);
        return AjaxResult.success(uploadVo);
    }

    @MvcData("上传多图片")
    @PostMapping(value = "/uploadMultiImgs", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult uploadMultiImgs(@ModelAttribute MultiImgsUploadDto dto) throws Exception {
        List<FileUploadVo> uploadVos = this.fileService.uploadMultiImg(dto);
        return AjaxResult.success(uploadVos);
    }

    @MvcData("上传多文件")
    @PostMapping(value = "/uploadMultiFiles", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult uploadMultiFiles(@ModelAttribute MultiFilesUploadDto dto) throws Exception {
        List<FileUploadVo> uploadVos = this.fileService.uploadMultiFiles(dto);
        return AjaxResult.success(uploadVos);
    }

    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult delete(@RequestParam(value = "filePath") String filePath) {
        boolean delete = this.fileService.delete(filePath);
        return AjaxResult.success(delete);
    }

    /**
     * 上传 普通文件
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/uploadFile", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult uploadFile(@ModelAttribute FileUploadDto dto) throws Exception {
        FileUploadVo uploadVo = this.fileService.uploadFile(dto);
        return AjaxResult.success(uploadVo);
    }

    @GetMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam(value = "filePath") String filePath) throws IOException {
        // 下载文件的绝对路径
        String absPath = this.fileService.buildAbsPath(filePath);
        //准备下载的文件
        File file = new File(absPath);
        if (!file.exists()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        String fileName = StringUtils.substringAfterLast(filePath, filePath.lastIndexOf("/") > 0 ? "/" : "\\");//文件名
        HttpHeaders headers = new HttpHeaders();//http头信息
        fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");//设置编码
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //MediaType:互联网媒介类型  contentType：具体请求中的媒体类型信息
        return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }
}
